package stripepay

import (
	"context"
	"errors"
	"fmt"
	"github.com/shopspring/decimal"
	"github.com/stripe/stripe-go/v80"
	"github.com/stripe/stripe-go/v80/refund"
	model2 "simplifly-tech/payment/model"
)

func (stripeService *StripeService) Refund(ctx context.Context, refundData *model2.RefundData) (string, error) {

	stripe.Key = stripeService.SecretKey
	// 要退款的支付 ID
	moneyFrom := refundData.Amount.Mul(decimal.NewFromFloat(100))
	money := moneyFrom.IntPart()

	// 创建退款
	params := &stripe.RefundParams{
		PaymentIntent: stripe.String(refundData.ReferenceID),
		Amount:        stripe.Int64(money), // 可选，退款金额（以最小货币单位表示，例如 cents）
	}

	// 申请退款
	refundDataReq, err := refund.New(params)
	if err != nil {
		fmt.Printf("Error creating refund: %v\n", err)
		return "", errors.New(err.Error())
	}

	return refundDataReq.ID, nil
}
